package org.example;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class WordFormatChecker {
    public static int firstQuestionCheck(XWPFDocument doc) {
        // 检查是否删除文中所有的空段
        boolean hasEmptyParagraphs = false;
        List<Integer> emptyParagraphPositions = new ArrayList<>();

        for (int i = 0; i < doc.getParagraphs().size() - 1; i++) {
            XWPFParagraph paragraph = doc.getParagraphs().get(i);
            String paragraphText = paragraph.getText().trim();

            // 检查段落是否为空（没有文本内容）
            if (paragraphText.isEmpty()) {
                hasEmptyParagraphs = true;
                emptyParagraphPositions.add(i + 1); // 记录第几段是空的
            }
        }

        if (hasEmptyParagraphs) {
            System.out.println("❌ 文档中仍存在空段，位置：第" + emptyParagraphPositions + "段");
        } else {
            System.out.println("✅ 所有空段已正确删除");
        }

        // 检查是否将文中“北京礼品”一词替换为“北京礼物”
        boolean foundUnreplacedText = false;
        List<String> unreplacedInstances = new ArrayList<>();

        // 检查所有段落
        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            String paragraphText = paragraph.getText();

            // 使用正则表达式查找未替换的"北京礼品"
            if (paragraphText.contains("北京礼品")) {
                foundUnreplacedText = true;

                // 找到具体的未替换实例
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    String runText = run.getText(0);
                    if (runText != null && runText.contains("北京礼品")) {
                        unreplacedInstances.add("'" + runText + "'");
                    }
                }
            }
        }

        // 检查表格中的文本
        for (XWPFTable table : doc.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph paragraph : cell.getParagraphs()) {
                        String paragraphText = paragraph.getText();
                        if (paragraphText.contains("北京礼品")) {
                            foundUnreplacedText = true;
                            unreplacedInstances.add("表格中的'" + paragraphText + "'");
                        }
                    }
                }
            }
        }

        // 已知“北京礼品”一词出现次数
        int liPinCount = 14;
        int liWuCount = 0;
        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            String paragraphText = paragraph.getText();
            // 统计"北京礼物"出现的次数（作为正确替换的指标）
            if (paragraphText.contains("北京礼物")) {
                int countWord = countWord(paragraphText, "北京礼物");
                liWuCount += countWord;
            }
        }
        for (XWPFTable table : doc.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph paragraph : cell.getParagraphs()) {
                        String paragraphText = paragraph.getText();
                        if (paragraphText.contains("北京礼物")) {
                            liWuCount++;
                        }
                    }
                }
            }
        }
        // 检查是否将文中“北京礼品”一词全部替换为“北京礼物”
        if (liWuCount != liPinCount) {
            foundUnreplacedText = true;
        }

        if (foundUnreplacedText) {
            System.out.println("❌ 文档中仍存在未替换的'北京礼品'，实例：" + unreplacedInstances);
        } else {
            System.out.println("✅ 所有'北京礼品'已正确替换为'北京礼物'");
        }

        return hasEmptyParagraphs && foundUnreplacedText ? 0 : 1;
    }

    public static int secondQuestionCheck(XWPFDocument doc) {
        int score = 0;

        // 获取第一段
        XWPFParagraph paragraph = doc.getParagraphs().get(0);
        // 获取段落属性
        CTPPr pPr = paragraph.getCTP().getPPr();
        // 检查是否居住对齐
        CTJc jc = pPr.getJc();
        if (jc != null && jc.getVal() == STJc.CENTER) {
            System.out.println("✅ 标题已居中");
        } else {
            System.out.println("❌ 标题未居中");
        }

        // 获取中文标题
        XWPFRun run = null;
        // 获取图片
        CTDrawing ctDrawing = null;
        boolean isLeftTitle = false;
        for (XWPFRun r : paragraph.getRuns()) {
            List<CTDrawing> drawingList = r.getCTR().getDrawingList();
            if (drawingList != null && !drawingList.isEmpty()) {
                ctDrawing = drawingList.get(0);
            }
            String text = r.getText(0);
            if (text != null && text.contains("北京礼物")) {
                run = r;
                isLeftTitle = ctDrawing != null;
                break;
            }
        }
        if (run != null) {
            CTRPr rPr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : null;
            if (rPr != null) {
                // 检查字体为小二号字
                List<CTHpsMeasure> szList = rPr.getSzList();
                if (szList != null && !szList.isEmpty()) {
                    CTHpsMeasure sz = szList.get(0);
                    if (sz != null && sz.getVal() != null && sz.getVal().toString().equals("36")) {
                        System.out.println("✅ 标题中文字体为小二号字");
                    } else {
                        System.out.println("❌ 标题中文字体不是小二号字");
                    }
                }
                // 检查文字颜色是否为红色
                List<CTColor> colorList = rPr.getColorList();
                if (colorList != null && !colorList.isEmpty()) {
                    CTColor color = colorList.get(0);
                    if (color != null && color.getVal() != null) {
                        String colorHex = getColorHex(color);
                        if (colorHex.equals("#FF0000")) {
                            System.out.println("✅ 标题中文颜色为红色");
                        } else {
                            System.out.println("❌ 标题中文颜色不是红色");
                        }
                    } else {
                        System.out.println("❌ 标题英文颜色未设置");
                    }
                }
                // 判断字体是否为黑体
                List<CTFonts> fontList = rPr.getRFontsList();
                if (fontList != null && !fontList.isEmpty()) {
                    CTFonts font = fontList.get(0);
                    if (font != null && font.getEastAsia() != null && font.getEastAsia().contains("黑体")) {
                        score++;
                        System.out.println("✅ 标题中文字体设置为黑体");
                    } else {
                        System.out.println("✗✅ 标题中文字体不是黑体");
                    }
                }
            }
        }
        // 获取英文标题
        run = null;
        for (XWPFRun r : paragraph.getRuns()) {
            String text = r.getText(0);
            if (text != null && text.contains("Beijing Gifts")) {
                run = r;
                break;
            }
        }
        if (run != null) {
            CTRPr rPr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : null;
            if (rPr != null) {
                // 检查字体为小二号字
                List<CTHpsMeasure> szList = rPr.getSzList();
                if (szList != null && !szList.isEmpty()) {
                    CTHpsMeasure sz = szList.get(0);
                    if (sz != null && sz.getVal() != null && sz.getVal().toString().equals("36")) {
                        System.out.println("✅ 标题英文字体为小二号字");
                    } else {
                        System.out.println("❌ 标题英文字体不是小二号字");
                    }
                }
                // 检查文字颜色是否为红色
                List<CTColor> colorList = rPr.getColorList();
                if (colorList != null && !colorList.isEmpty()) {
                    CTColor color = colorList.get(0);
                    if (color != null && color.getVal() != null) {
                        String colorHex = getColorHex(color);
                        if (colorHex.equals("#FF0000")) {
                            System.out.println("✅ 标题英文颜色为红色");
                        } else {
                            System.out.println("❌ 标题英文颜色不是红色");
                        }
                    } else {
                        System.out.println("❌ 标题英文颜色未设置");
                    }
                }
                // 判断字体是否为黑体
                List<CTFonts> fontList = rPr.getRFontsList();
                if (fontList != null && !fontList.isEmpty()) {
                    CTFonts font = fontList.get(0);
                    if (font != null && font.getAscii() != null && font.getAscii().contains("Times New Roman")) {
                         System.out.println("✅ 标题英文字体设置为Times New Roman");
                    } else {
                        XWPFStyles styles = paragraph.getDocument().getStyles();
                        XWPFDefaultRunStyle defaultRunStyle = styles.getDefaultRunStyle();
                        Class<? extends XWPFDefaultRunStyle> aClass = defaultRunStyle.getClass();
                        try {
                            Method getRPr = aClass.getDeclaredMethod("getRPr");
                            getRPr.setAccessible(true);
                            CTRPr rPr1 = (CTRPr) getRPr.invoke(defaultRunStyle);
                            List<CTFonts> rFontsList = rPr1.getRFontsList();
                            if (rFontsList != null && !rFontsList.isEmpty()) {
                                CTFonts rFonts = rFontsList.get(0);
                                if (rFonts != null && rFonts.getAscii() != null && rFonts.getAscii().contains("Times New Roman")) {
                                    System.out.println("✅ 标题英文字体设置为Times New Roman");
                                } else {
                                    System.out.println("❌ 标题英文字体不是Times New Roman");
                                }
                            } else {
                                System.out.println("❌ 标题英文字体不是Times New Roman");
                            }
                        } catch (Exception e) {
                            System.out.println("❌ 标题英文字体不是Times New Roman");
                        }
                    }
                }
                // 判断是否设置圆点型着重号
                List<CTEm> emList = rPr.getEmList();
                if (emList != null && !emList.isEmpty()) {
                    CTEm em = emList.get(0);
                    if (em.getVal() == STEm.DOT) {
                        System.out.println("✅ 标题英文设置圆点型着重号");
                    } else {
                        System.out.println("❌ 标题英文没有设置圆点型着重号");
                    }
                } else {
                    System.out.println("❌ 标题英文没有设置着重号");
                }
            }
        }
        // 判断图片是否在标题左侧
        if (isLeftTitle) {
            List<CTInline> inlineList = ctDrawing.getInlineList();
            if (inlineList != null && !inlineList.isEmpty()) {
                CTInline ctInline = inlineList.get(0);
                CTNonVisualDrawingProps docPr = ctInline.getDocPr();
                if (docPr != null && docPr.getDescr().equals("gift")) {
                    System.out.println("✅ 图片gift插入在标题左侧");
                } else {
                    System.out.println("❌ 图片gift没有插入在标题左侧");
                }
            }
        } else {
            System.out.println("❌ 图片没有在标题左侧");
        }

        return score;
    }

    public static int thirdQuestionCheck(XWPFDocument doc) {
        int score = 0;
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        if (paragraphs != null && !paragraphs.isEmpty()) {
            int size = paragraphs.size();
            size = Math.min(size, 6);
            for (int i = 1; i < size; i++) {
                System.out.println("第" + (i + 1) + "段");
                // 获取第一段
                XWPFParagraph paragraph = paragraphs.get(i);
                // 获取段落属性
                CTPPr pPr = paragraph.getCTP().getPPr();
                CTParaRPr rPr = pPr.getRPr();
                // 获取颜色
                List<CTColor> colorList = rPr.getColorList();
                if (colorList != null && !colorList.isEmpty()) {
                    CTColor color = colorList.get(0);
                    if (color != null && color.getVal() != null) {
                        String colorHex = getColorHex(color);
                        if (colorHex.equals("#0070C0")) {
                            System.out.println("✅ 段落颜色为蓝色");
                        } else {
                            System.out.println("❌ 段落颜色不是蓝色");
                        }
                    } else {
                        System.out.println("❌ 段落颜色未设置");
                    }
                } else {
                    System.out.println("❌ 段落颜色未设置");
                }
                // 获取字号
                List<CTHpsMeasure> szList = rPr.getSzList();
                if (szList != null && !szList.isEmpty()) {
                    CTHpsMeasure sz = szList.get(0);
                    if (sz != null && sz.getVal() != null && sz.getVal().toString().equals("24")) {
                        System.out.println("✅ 段落字号为小四号字");
                    } else {
                        System.out.println("❌ 段落字号不是小四号字");
                    }
                } else {
                    System.out.println("❌ 段落字号未设置");
                }
                // 获取首行缩进
                CTInd ind = pPr.getInd();
                if (ind != null) {
                    BigInteger firstLineChars = ind.getFirstLineChars();
                    // 判断是否首行缩进两字符
                    if (firstLineChars != null && firstLineChars.intValue() == 200) {
                        System.out.println("✅ 段落首行缩进两字符");
                    } else {
                        System.out.println("❌ 段落首行缩进不是两字符");
                    }
                } else {
                    System.out.println("❌ 段落首行缩进未设置");
                }

                CTSpacing spacing = pPr.getSpacing();
                if (spacing != null) {
                    // 获取段前间距
                    BigInteger beforeLines = spacing.getBeforeLines();
                    // 判断段前间距是否为0.5行
                    if (beforeLines != null && beforeLines.intValue() == 50) {
                        System.out.println("✅ 段前间距为0.5行");
                    } else {
                        System.out.println("❌ 段前间距不是0.5行");
                    }
                    // 获取行间距
                    STLineSpacingRule.Enum lineRule = spacing.getLineRule();
                    Object line = spacing.getLine();
                    // 判断行间距是否为1.5倍行距
                    if (lineRule.intValue() == STLineSpacingRule.INT_AUTO && line != null && line.toString().equals("360")) {
                        System.out.println("✅ 段间距为1.5倍行距");
                    } else {
                        System.out.println("❌ 段间距不是1.5倍行距");
                    }
                }
            }
        }
        return score;
    }

    public static int fourthQuestionCheck(XWPFDocument doc) {
        int score = 0;

        // 获取第六段
        XWPFParagraph paragraph = doc.getParagraphs().get(6);
        // 获取段落属性
        CTPPr pPr = paragraph.getCTP().getPPr();
        // 判断是否为表格标题
        CTOnOff keepNext = pPr.getKeepNext();
        if (keepNext != null) {
            System.out.println("✅ 表格标题已设置");
        } else {
            System.out.println("❌ 表格标题未设置");
        }
        // 检查是否居住对齐
        CTJc jc = pPr.getJc();
        if (jc != null && jc.getVal() == STJc.CENTER) {
            System.out.println("✅ 表格标题已居中");
        } else {
            System.out.println("❌ 表格标题未居中");
        }

        // 获取表格标题
        XWPFRun run = null;
        for (XWPFRun r : paragraph.getRuns()) {
            String text = r.getText(0);
            if (text != null && text.contains("“北京礼物”连锁店一览表")) {
                run = r;
                break;
            }
        }
        if (run != null) {
            CTRPr rPr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : null;
            if (rPr != null) {
                // 判断是否为小三字号
                List<CTHpsMeasure> szList = rPr.getSzList();
                if (szList != null && !szList.isEmpty()) {
                    CTHpsMeasure sz = szList.get(0);
                    if (sz != null && sz.getVal() != null && sz.getVal().toString().equals("30")) {
                        System.out.println("✅ 表格标题字号为小三号字");
                    } else {
                        System.out.println("❌ 表格标题字号不是小三号字");
                    }
                }
                // 判断字体是否为楷体
                List<CTFonts> fontList = rPr.getRFontsList();
                if (fontList != null && !fontList.isEmpty()) {
                    CTFonts font = fontList.get(0);
                    if (font != null && font.getEastAsia() != null && font.getEastAsia().equals("楷体")) {
                        System.out.println("✅ 表格标题字体为楷体");
                    } else {
                        System.out.println("❌ 表格标题字体不是楷体");
                    }
                }
                // 判断是否为红色
                List<CTColor> colorList = rPr.getColorList();
                if (colorList != null && !colorList.isEmpty()) {
                    CTColor color = colorList.get(0);
                    if (color != null && color.getVal() != null && getColorHex(color).equals("#EE0000")) {
                        System.out.println("✅ 表格标题已设置为红色");
                    } else {
                        System.out.println("❌ 表格标题未设置为红色");
                    }
                }
            }
        }

        List<XWPFTable> tables = doc.getTables();
        if (tables != null && !tables.isEmpty()) {
            XWPFTable xwpfTable = tables.get(0);
            CTTbl ctTbl = xwpfTable.getCTTbl();
            CTTblPr tblPr = ctTbl.getTblPr();
            if (tblPr != null) {
                CTTblBorders tblBorders = tblPr.getTblBorders();
                if (tblBorders != null) {
                    CTBorder top = tblBorders.getTop();
                    CTBorder left = tblBorders.getLeft();
                    CTBorder bottom = tblBorders.getBottom();
                    CTBorder right = tblBorders.getRight();

                    if (getColorHex(top.getColor()).equals("#0070C0") &&
                                    getColorHex(left.getColor()).equals("#0070C0") &&
                                    getColorHex(bottom.getColor()).equals("#0070C0") &&
                                    getColorHex(right.getColor()).equals("#0070C0")
                    ) {
                        System.out.println("✅ 表格外边框已设置为蓝色");
                    } else {
                        System.out.println("❌ 表格外边框不是蓝色");
                    }

                    if (top.getVal() == STBorder.DOUBLE &&
                                    left.getVal() == STBorder.DOUBLE &&
                                    bottom.getVal() == STBorder.DOUBLE &&
                                    right.getVal() == STBorder.DOUBLE
                    ) {
                        System.out.println("✅ 表格外边框已设置为双细线");
                    } else {
                        System.out.println("❌ 表格外边框不是双细线");
                    }

                    if (top.getSz().toString().equals("4") &&
                                left.getSz().toString().equals("4") &&
                                bottom.getSz().toString().equals("4") &&
                                right.getSz().toString().equals("4")
                    ) {
                        System.out.println("✅ 表格外边框大小已设置为0.5磅");
                    } else {
                        System.out.println("❌ 表格外边框大小不是0.5磅");
                    }

                    CTBorder insideH = tblBorders.getInsideH();
                    CTBorder insideV = tblBorders.getInsideV();
                    if (getColorHex(insideH.getColor()).equals("#0070C0") &&
                            getColorHex(insideV.getColor()).equals("#0070C0")
                    ) {
                        System.out.println("✅ 表格内边框已设置为蓝色");
                    } else {
                        System.out.println("❌ 表格内边框不是蓝色");
                    }

                    if (insideH.getVal() == STBorder.SINGLE &&
                            insideV.getVal() == STBorder.SINGLE
                    ) {
                        System.out.println("✅ 表格内边框已设置为单细线");
                    } else {
                        System.out.println("❌ 表格内边框不是单细线");
                    }

                    if (insideH.getSz().toString().equals("6") &&
                            insideV.getSz().toString().equals("6")
                    ) {
                        System.out.println("✅ 表格内边框大小已设置为0.75磅");
                    } else {
                        System.out.println("❌ 表格内边框大小不是0.75磅");
                    }
                } else {
                    System.out.println("❌ 表格边框未设置");
                }
            } else {
                System.out.println("❌ 表格属性未设置");
            }
            // 获取所有行
            List<CTRow> trList = ctTbl.getTrList();
            for (int i = 0; i < trList.size(); i++) {
                CTRow ctRow = trList.get(i);
                // 第一行
                if (i == 0) {
                    List<CTTc> tcList = ctRow.getTcList();
                    for (int j = 0; j < tcList.size(); j++) {
                        CTTc ctTc = tcList.get(j);
                        CTTcPr tcPr = ctTc.getTcPr();
                        if (tcPr != null) {
                            CTShd shd = tcPr.getShd();
                            if (shd != null) {
                                Object fill = shd.getFill();
                                if (fill != null) {
                                    if (getColorHex(fill).equals("#92D050")) {
                                        System.out.println("✅ 表格第" + (i + 1) +"行第"+ (j + 1) +"列已设置为浅绿色");
                                    } else {
                                        System.out.println("❌ 表格第" + (i + 1) +"行第"+ (j + 1) +"列未设置为浅绿色");
                                    }
                                }
                            } else {
                                System.out.println("❌ 表格第" + (i + 1) +"行第"+ (j + 1) +"列未设置背景色");
                            }
                        }
                    }
                } else {
                    List<CTTc> tcList = ctRow.getTcList();
                    // 获取第一列
                    CTTc ctTc = tcList.get(0);
                    CTTcPr tcPr = ctTc.getTcPr();
                    if (tcPr != null) {
                        CTShd shd = tcPr.getShd();
                        if (shd != null) {
                            Object fill = shd.getFill();
                            if (fill != null) {
                                if (getColorHex(fill).equals("#92D050")) {
                                    System.out.println("✅ 表格第" + (i + 1) +"行第1列已设置为浅绿色");
                                } else {
                                    System.out.println("❌ 表格第" + (i + 1) +"行第1列未设置为浅绿色");
                                }
                            }
                        } else {
                            System.out.println("❌ 表格第" + (i + 1) +"行第1列未设置背景色");
                        }
                    } else {
                        System.out.println("❌ 表格第" + (i + 1) +"行第1列未设置背景色");
                    }
                }
            }
        } else {
            System.out.println("❌ 表格不存在");
        }
        return score;
    }

    public static int fifthQuestionCheck(XWPFDocument doc) {
        int score = 0;
        List<XWPFTable> tables = doc.getTables();
        if (tables != null && !tables.isEmpty()) {
            XWPFTable xwpfTable = tables.get(0);
            CTTbl ctTbl = xwpfTable.getCTTbl();
            CTTblGrid tblGrid = ctTbl.getTblGrid();
            if (tblGrid != null) {
                List<CTTblGridCol> gridColList = tblGrid.getGridColList();
                for (int i = 0; i < gridColList.size(); i++) {
                    CTTblGridCol ctGridCol = gridColList.get(i);
                    if (ctGridCol != null) {
                        switch (i) {
                            case 0:
                                if (ctGridCol.getW().toString().equals("850")) {
                                    System.out.println("✅ 表格第" + (i + 1) +"列已设置为15磅");
                                } else {
                                    System.out.println("❌ 表格第" + (i + 1) +"列未设置为15磅");
                                }
                                break;
                            case 1:
                                if (ctGridCol.getW().toString().equals("2551")) {
                                    System.out.println("✅ 表格第" + (i + 1) +"列已设置为45磅");
                                } else {
                                    System.out.println("❌ 表格第" + (i + 1) +"列未设置为45磅");
                                }
                                break;
                            case 2:
                                if (ctGridCol.getW().toString().equals("5386")) {
                                    System.out.println("✅ 表格第" + (i + 1) +"列已设置为95磅");
                                } else {
                                    System.out.println("❌ 表格第" + (i + 1) +"列未设置为95磅");
                                }
                                break;
                            case 3:
                                if (ctGridCol.getW().toString().equals("1134")) {
                                    System.out.println("✅ 表格第" + (i + 1) +"列已设置为20磅");
                                } else {
                                    System.out.println("❌ 表格第" + (i + 1) +"列未设置为20磅");
                                }
                        }
                    }
                }
            } else {
                System.out.println("❌ 表格列属性未设置");
            }

            CTTblPr tblPr = ctTbl.getTblPr();
            if (tblPr != null) {
                // 判断整体居中
                CTJcTable jc = tblPr.getJc();
                if (jc != null && jc.getVal() == STJcTable.CENTER) {
                    System.out.println("✅ 表格已设置为居中");
                } else {
                    System.out.println("❌ 表格未设置为居中");
                }
            } else {
                System.out.println("❌ 表格属性未设置");
            }
            // 获取所有行
            List<CTRow> trList = ctTbl.getTrList();
            // 第一行
            CTRow ctRow = trList.get(0);
            List<CTTc> tcList = ctRow.getTcList();
            for (int j = 0; j < tcList.size(); j++) {
                CTTc ctTc = tcList.get(j);
                CTTcPr tcPr = ctTc.getTcPr();
                CTVerticalJc vAlign = tcPr.getVAlign();
                boolean bottom = vAlign.getVal() == STVerticalJc.BOTTOM;
                boolean center = false;
                boolean bold = false;

                List<CTP> pList = ctTc.getPList();
                if (pList != null && !pList.isEmpty()) {
                    CTP ctp = pList.get(0);
                    CTPPr pPr = ctp.getPPr();
                    if (pPr != null) {
                        CTJc jc = pPr.getJc();
                        center = jc.getVal() == STJc.CENTER;
                    }

                    List<CTR> rList = ctp.getRList();
                    if (rList != null && !rList.isEmpty()) {
                        CTR ctr = rList.get(0);
                        CTRPr rPr = ctr.getRPr();
                        if (rPr != null) {
                            List<CTOnOff> bList = rPr.getBList();
                            bold = bList.size() > 0;
                        }
                    }
                }

                if (bottom && center) {
                    System.out.println("✅ 表格第1行第"+ (j + 1) +"列已设置为靠下居中对齐");
                } else {
                    System.out.println("❌ 表格第1行第"+ (j + 1) +"列未设置为靠下居中对齐");
                }

                if (bold) {
                    System.out.println("✅ 表格第1行第"+ (j + 1) +"列已设置为加粗");
                } else {
                    System.out.println("❌ 表格第1行第"+ (j + 1) +"列未设置为加粗");
                }
            }


            for (int i = 1; i < trList.size(); i++) {
                CTRow ctRow1 = trList.get(i);
                List<CTTc> tcList1 = ctRow1.getTcList();
                if (tcList1 != null && !tcList1.isEmpty()) {
                    // 第i行第一列
                    CTTc ctTc = tcList1.get(0);
                    CTTcPr tcPr = ctTc.getTcPr();
                    CTVerticalJc vAlign = tcPr.getVAlign();
                    boolean alignCenter = vAlign.getVal() == STVerticalJc.CENTER;
                    boolean center = false;

                    List<CTP> pList = ctTc.getPList();
                    if (pList != null && !pList.isEmpty()) {
                        CTP ctp = pList.get(0);
                        CTPPr pPr = ctp.getPPr();
                        if (pPr != null) {
                            CTJc jc = pPr.getJc();
                            center = jc.getVal() == STJc.CENTER;
                        }
                    }

                    if (alignCenter && center) {
                        System.out.println("✅ 表格第" + (i + 1) +"行第1列已设置为居中对齐");
                    } else {
                        System.out.println("❌ 表格第" + (i + 1) +"行第1列未设置为居中对齐");
                    }
                } else {
                    System.out.println("❌ 表格第" + (i + 1) +"行第1列未设置为居中对齐");
                }
            }
        } else {
            System.out.println("❌ 表格不存在");
        }
        return score;
    }

    public static void main(String[] args) {
        try {
            XWPFDocument doc = new XWPFDocument(
                    new FileInputStream("E:\\存放文件\\驿站\\工作目录\\开发项目\\2025\\顺德学院\\一级考试\\wps.docx")
            );
            // 开始计时
            long startTime = System.currentTimeMillis();
            // 第一题检查
            System.out.println("\n=== 第一题检查 ===");
            firstQuestionCheck(doc);
            // 第二题检查
            System.out.println("\n=== 第二题检查 ===");
            secondQuestionCheck(doc);
            // 第三题检查
            System.out.println("\n=== 第三题检查 ===");
            thirdQuestionCheck(doc);
            // 第四题检查
            System.out.println("\n=== 第四题检查 ===");
            fourthQuestionCheck(doc);
            // 第五题检查
            System.out.println("\n=== 第五题检查 ===");
            fifthQuestionCheck(doc);
            // 结束计时
            long endTime = System.currentTimeMillis();
            System.out.println("\n耗时: " + (endTime - startTime) + "毫秒");
        } catch (Exception e) {
            System.out.println("文档检查失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 将CTColor的颜色值转换为十六进制字符串
     */
    public static String getColorHex(CTColor color) {
        if (color == null || color.getVal() == null) {
            return null;
        }
        return getColorHex((byte[]) color.getVal());
    }

    public static String getColorHex(Object colorBytes) {
        return getColorHex((byte[]) colorBytes);
    }


    public static String getColorHex(byte[] colorBytes) {
        if (colorBytes == null || colorBytes.length < 3) {
            return null;
        }

        // 将有符号字节转换为无符号整数
        int red = colorBytes[0] & 0xFF;
        int green = colorBytes[1] & 0xFF;
        int blue = colorBytes[2] & 0xFF;
        return String.format("#%02X%02X%02X", red, green, blue);
    }

    public static int countWord(String text, String word) {
        if (text == null || word == null || word.isEmpty()) {
            return 0;
        }

        int count = 0;
        int index = 0;

        while ((index = text.indexOf(word, index)) != -1) {
            count++;
            index += word.length(); // 移动到匹配词后面继续搜索
        }

        return count;
    }
}